const {
  req
} = require('../../utils/requests')

Page({

  data: {
    playid: '',
    playerInfo: [], //基本数据
    playerHeader: [], //头部数据
    nTeamDetails: [], //常规赛数据
    nPlayCareer: [], //生涯数据
    nPlayMatch: [], //比赛数据
    flagRegularSeason: 0, //常规赛切换状态
    columns: [],
    showText: '',
    show: false,
  },


  onLoad(options) {
    this.setData({
      playid: JSON.parse(options.playid)
    })
    this.init()
  },
  init() {
    let self = this
    // 获取基本信息
    req('https://matchweb.sports.qq.com/playerUtil/playerInfo?from=h5&competitionId=100000&playerId=' + self.data.playid + '&usage=').then(res => {
      self.setData({
        playerInfo: res.data.data
      })
      console.log(self.data.playerInfo)
      // 给生涯赋值
      let datas = self.data.playerInfo.competitonSeason[100000]
      var competitonSeasonList = []
      datas.forEach(function (item, index) {
        competitonSeasonList.push(item.name)
      })
      self.setData({
        columns: competitonSeasonList,
        showText: competitonSeasonList[0]
      })
    })
    // 页面顶部基本信息
    req('https://matchweb.sports.qq.com/playerUtil/playerHeader?from=h5&competitionId=100000&playerId=' + self.data.playid + '&backflow=undefined&hideInfo=true').then(res => {
      self.setData({
        playerHeader: res.data.data
      })
    })
    // 页面顶部基本信息
    req('https://matchweb.sports.qq.com/playerUtil/nPlayDetails?from=h5&playerId=' + self.data.playid + '&competitionId=100000&seasonId=2022&seasonType=playoff&teamId=13').then(res => {
      self.setData({
        nTeamDetails: res.data.data
      })
    })

    // 生涯数据
    req('https://matchweb.sports.qq.com/playerUtil/nPlayCareer?from=h5&playerId=' + self.data.playid + '&competitionId=100000').then(res => {
      self.setData({
        nPlayCareer: res.data.data
      })
    })

    // 比赛涯数据
    req('https://matchweb.sports.qq.com/playerUtil/nPlayMatch?from=h5&playerId=' + self.data.playid + '&competitionId=100000&seasonId=2022&seasonType=playoff&dataSource=NBA').then(res => {
      // console.log(res.data.data);
      self.setData({
        nPlayMatch: res.data.data
      })
    })

  },
  // 选择改变赛季比赛数据
  onChange(event) {
    let self = this
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      showText: value
    })
    let competitionList = self.data.playerInfo.competitonSeason[100000][index]
    // 比赛涯数据
    req('https://matchweb.sports.qq.com/playerUtil/nPlayMatch?from=h5&playerId=' + self.data.playid + '&competitionId=' + competitionList.competitionId + '&seasonId=' + competitionList.seasonId + '&seasonType=' + competitionList.seasonType + '&dataSource=NBA').then(res => {
      self.setData({
        nPlayMatch: res.data.data
      })
      self.onClose()
    })

  },
  // 开启滑动框
  showPopup() {
    this.setData({
      show: true
    });
  },
  // 关闭滑动块
  onClose() {
    this.setData({
      show: false
    });
  },
  // 回到球队页面
  gotoTeam(e) {
    let playid = this.data.playerInfo.competitonSeason[100000][0].teamId
    wx.navigateTo({
      url: '../team/team?playid=' + playid,
    })
  },
  // 球队详情
  gotoTeams(e) {
    let playid = e.currentTarget.dataset.teamid
    wx.navigateTo({
      url: '../team/team?playid=' + playid,
    })
  }

})